<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class ExcelExpoter extends AbstractExporter
{
    public function export()
    {
        Excel::create('Filename', function($excel) {

            $excel->sheet('Sheetname', function($sheet) {

                // 这段逻辑是从表格数据中取出需要导出的字段
                $rows = collect($this->getData())->map(function ($item) {

                    $item['name'] = $item['adverti']['name'];
                    return array_only($item, ['name', 'nickname', 'mobile', 'address']);
                });
                $title = ['nickname' => '名称', 'mobile' => '手机', 'address' => '地址', 'name' => '奖品'];
                $data = $rows->toArray();
                array_unshift($data, $title);

                $sheet->rows($data);

            });

        })->export('xls');
    }
}